package com.hengtuo.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class ObtainNetwork {
	
		public static boolean isNetAvailable(Context context) {

			ConnectivityManager connM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
					
			NetworkInfo netInfo = connM.getActiveNetworkInfo();

			if (netInfo == null || !netInfo.isAvailable())
				return false;

			return true;
		}

		
		
		public static String getJsonFromWeb(String is,Context context) {
			URL url = null;

			HttpURLConnection urlConn = null;

			BufferedReader br = null;

			StringBuffer sb = new StringBuffer();

			try {
				url = new URL(is);

				urlConn = (HttpURLConnection) url.openConnection();

				urlConn.setConnectTimeout(5 * 1000);
				urlConn.setReadTimeout(5 * 1000);

				if (urlConn.getResponseCode() == 200) {


					br = new BufferedReader(new InputStreamReader(
							urlConn.getInputStream(), "utf-8"));

					String str = null;
					while ((str = br.readLine()) != null) {
						sb.append(str);

					}

				}else{

					Toast.makeText(context, "当前无网络", Toast.LENGTH_SHORT).show();

				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return sb.toString();

		}
		
		
		
		
}
